package game.metalmax.animation
{
	import flash.geom.Point;
	
	import game.metalmax.animation.zone.SceneZone;
	import game.metalmax.entity.character.Character;
	import game.metalmax.entity.character.Vehicle;
	import game.metalmax.events.MetalMaxEvent;
	import game.metalmax.module.character.CharacterManager;
	import game.metalmax.module.world.WorldProxy;
	
	import showtime.framework.engine.World2D;
	import showtime.framework.engine.map.MapObjectType;
	import showtime.framework.engine.map.rpg.ObjectHolder;
	import showtime.framework.events.GameEvent;
	import showtime.framework.events.MessageDispatcher;
	import showtime.framework.mvc.patterns.facade.Facade;

	public class HeroSOB extends CharacterSOB
	{ 
		public function HeroSOB(source:Character)
		{
			super(source);
			
			play(CharacterManager.getInstance().self.animationName);
		
			MessageDispatcher.getInstance().addEventListener(MetalMaxEvent.TRANSFER_ON_TANK, transferOnTank);
			MessageDispatcher.getInstance().addEventListener(MetalMaxEvent.TRANSFER_OFF_TANK, transferOffTank);
		}
		
		override public function destroy():void
		{
			super.destroy();
			
			MessageDispatcher.getInstance().removeEventListener(MetalMaxEvent.TRANSFER_ON_TANK, transferOnTank);
			MessageDispatcher.getInstance().removeEventListener(MetalMaxEvent.TRANSFER_OFF_TANK, transferOffTank);
		}
		
		override public function click():void
		{
			super.click();
		}
	
		override public function setFollowTarget(source:Character, position:Point):void
		{			
			followTarget = new HeroSOB(source);			
			
			followTarget.play(getAnimationName());
			followTarget.x = position.x;
			followTarget.y = position.y;
			followTarget.targetX = position.x;
			followTarget.targetY = position.y;
			
			followTarget.direction = direction;
			followTarget.lastDirection = direction;
			SceneZone(World2D.scene).map.addObect(followTarget);
		}
		
		override public function moveStop():void
		{
			super.moveStop();
			
			if (followTarget == null)
			{
				MessageDispatcher.getInstance().dispatchEvent(new GameEvent(GameEvent.OBJECT_GROUP_MOVE_STOP, this));				
			}
		}
		
		override public function configAvatar():void
		{
			if (Character(source).vehicle != null)
			{
				source.animationConfigID = Character(source).vehicle.animationConfigID;
				source.animationName = Character(source).vehicle.animationName;
				source.graphicID = Character(source).vehicle.graphicID;
			}
			
			super.configAvatar();		
		}
		
		private function transferOnTank(evt:MetalMaxEvent):void
		{
			if (evt.data.hero != this)
			{
				return;
			}
	
			var vehicle:Vehicle = new Vehicle();
			vehicle.animationConfigID = evt.data.tank.source.animationConfigID;
			vehicle.type = MapObjectType.VEHICLE;
			vehicle.id = evt.data.tank.source.id;
			vehicle.name = evt.data.tank.source.name;
			vehicle.animationName = lastAnimationName;
			vehicle.graphicID = evt.data.tank.source.graphicID;
			Character(source).vehicle = vehicle;

			source.animationConfigID = vehicle.animationConfigID;
			source.animationName = lastAnimationName;
			source.graphicID = vehicle.graphicID;
			
			configAvatar();

			WorldProxy(Facade.getInstance().retrieveProxy(String(WorldProxy))).transferOnC2S(source.id, source.animationConfigID, source.animationName, source.graphicID, vehicle.id);
		}
		
		private function transferOffTank(evt:MetalMaxEvent):void
		{
			if (evt.data == this)
			{
				var vehicle:Vehicle = Character(source).vehicle;			
				Character(source).vehicle = null;
				
				var object:ObjectHolder = ObjectHolder.generate(vehicle);	
				object.x = x;
				object.y = y;
				object.properties["animationName"] = lastAnimationName;			
				
				source.animationConfigID = Character.getAnimationConfigID();
				source.graphicID = Character.getGraphicIDByName(source.name);
				
				configAvatar();
				
				MessageDispatcher.getInstance().dispatchEvent(new GameEvent(GameEvent.SCENE_OBJECT_ADD, [object]));
				
				WorldProxy(Facade.getInstance().retrieveProxy(String(WorldProxy))).transferOffC2S(source.id, x, y);
			}
			
		}
	}
}